package com.ddmmo.exhibition.localBOSS.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddmmo.exhibition.common.entity.Result;
import com.ddmmo.exhibition.constant.SystemConstant;
import com.ddmmo.exhibition.localBOSS.project.entity.po.TbProjectDataPO;
import com.ddmmo.exhibition.localBOSS.project.mapper.TbProjectDataMapper;
import com.ddmmo.exhibition.localBOSS.project.service.ProjectDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台管理-所属项目数据详情 - Impl
 * </p>
 *
 * @author JiangYiji
 * @since 2024/10/30
 */
@Slf4j
@Service
public class ProjectDataServiceImpl extends ServiceImpl<TbProjectDataMapper, TbProjectDataPO> implements ProjectDataService {

    @Resource
    private TbProjectDataMapper tbProjectDataMapper;

    /*
     * 根据分类ID查询对应项目数据详情
     */
    @Override
    public List<TbProjectDataPO> selectProjectDataById(String classifyId) {
        QueryWrapper<TbProjectDataPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classify_id", classifyId);
        queryWrapper.eq("del_flag", SystemConstant.NO_DELETE);
        List<TbProjectDataPO> tbProjectDataPOS = tbProjectDataMapper.selectList(queryWrapper);

        return tbProjectDataPOS;

    }

    /*
     * 添加项目数据详情
     */
    @Override
    public Result addProjectData(List<TbProjectDataPO> projectDataPOs) {

        int success = 0;
        Map<String,String> error = null;
        for (TbProjectDataPO projectDataPO : projectDataPOs){
            projectDataPO.setDelFlag(SystemConstant.NO_DELETE);
            projectDataPO.setCreateTime(new Date());
            projectDataPO.setCreateBy(SystemConstant.USER);
            projectDataPO.setUpdateTime(new Date());
            projectDataPO.setUpdateBy(SystemConstant.USER);
            int insert = tbProjectDataMapper.insert(projectDataPO);
            if (insert > 0) {
                success++;
            } else {
                error.put(projectDataPO.getVideoUrl(),projectDataPO.getPictureUrl());
            }
        }
        log.info("【添加项目数据详情】添加成功"+success+"条");
        log.error("【添加项目数据详情】添加失败" + error);
        return Result.success("【添加项目数据详情】添加成功"+success+"条---------【添加项目数据详情】添加失败" + error);
    }

    /*
     * 根据ID删除对应项目数据详情
     */
    @Override
    public Result deleteProjectDataById(String id) {
    int delete = tbProjectDataMapper.deleteById(id);
        if (delete > 0) {
            return Result.success("【删除项目数据详情】删除成功");
        } else {
            return Result.error("【删除项目数据详情】删除失败");
        }
    }

}
